package axht.trade.android.storage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class SharedPreferencesActivity extends AppCompatActivity {

    private TextView tv_key;

    private TextView tv_value;

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        tv_key = findViewById(R.id.et_key);
        tv_value = findViewById(R.id.et_value);
        sp = this.getSharedPreferences("sp", MODE_PRIVATE);
    }

    /**
     * 保存数据到Shared_Preferences
     *
     * @param view
     */
    public void save(View view) {
        //读取TextView的key和value
        String key = tv_key.getText().toString();
        String value = tv_value.getText().toString();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key,value);
        editor.commit();
    }

    /**
     * 读取数据到Shared_Preferences
     *
     * @param view
     */
    public void read(View view) {
        String key = tv_key.getText().toString();
        String value = sp.getString(key, "默认值");
        tv_value.setText(value);
    }
}
